package xworker.gluon.attach;

import com.gluonhq.attach.storage.StorageService;
import org.xmeta.ActionContext;
import org.xmeta.Thing;

import java.io.File;
import java.util.Optional;

public class StorageServiceActions {
    public static StorageService createStorageService(ActionContext actionContext){
        Optional<StorageService> st = StorageService.create();
        return st.orElse(null);
    }

    public static File getPrivateStorage(ActionContext actionContext){
        StorageService ss = StorageService.create().orElse(null);
        if(ss != null){
            return ss.getPrivateStorage().orElse(null);
        }else{
            return null;
        }
    }

    public static File getPublicStorage(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String subdirectory = self.doAction("getSubdirectory", actionContext);

        StorageService ss = StorageService.create().orElse(null);
        if(ss != null){
            return ss.getPublicStorage(subdirectory).orElse(null);
        }else{
            return null;
        }
    }

    public static boolean isExternalStorageReadable(ActionContext actionContext){
        StorageService ss = StorageService.create().orElse(null);
        if(ss != null){
            return ss.isExternalStorageReadable();
        }else{
            return false;
        }
    }

    public static boolean isExternalStorageWritable(ActionContext actionContext){
        StorageService ss = StorageService.create().orElse(null);
        if(ss != null){
            return ss.isExternalStorageWritable();
        }else{
            return false;
        }
    }
}
